package com.jtcl;

import cn.hutool.core.util.IdUtil;
import com.jtcl.common.utils.JacksonUtils;
import com.jtcl.module.entity.Org;
import com.jtcl.module.entity.User;
import com.jtcl.module.mapper.mongo.OrgMongoMapper;
import com.jtcl.module.mapper.mongo.UserMongoMapper;
import com.jtcl.module.retrofit.HttpAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sjt
 * @since 2021-01-25 10:35
 */
@Slf4j
@RestController
@RequestMapping("test")
public class TestRest {

    /**
     * 客户数据接口注入
     */
    @Autowired
    private UserMongoMapper userMongoMapper;
    @Autowired
    private OrgMongoMapper orgMongoMapper;

    @Autowired
    HttpAPI httpAPI;

    @PostMapping("test6")
    public Object test6(HttpServletRequest request) {
        String data="(true or false) || (true && true)";
        SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
        Expression expression = spelExpressionParser.parseExpression(data);
        Object value = expression.getValue();
        return value;
    }

    @PostMapping("test5")
    public Object test5(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return null;
    }


    @GetMapping("test4")
    public Object test4() {
        return httpAPI.health();
    }

    @GetMapping("test3")
    public Object test3() {
        User user1 = new User("1", 1, true);
        User user2 = new User("2", 2, true);
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        Org org = new Org("4", "4", users);
        Org save = orgMongoMapper.save(org);
        return save;
    }


    @GetMapping("test2")
    public Object test2() {
        User save = userMongoMapper.save(new User("6", 6, true, new Org(IdUtil.simpleUUID(), "org6")));
        log.info("test2-> {}", JacksonUtils.writeValueAsString(save));

        //此处是根据指定条件精确查询
        ExampleMatcher matcher = ExampleMatcher
                .matching()
                .withMatcher("org.name", ExampleMatcher.GenericPropertyMatchers.contains());
        User userQuery = new User();
        Org org = new Org("org");
        userQuery.setOrg(org);
        Example<User> userExample = Example.of(userQuery, matcher);

        List<User> all = userMongoMapper.findAll(userExample);
        log.info("test2->find:{}", JacksonUtils.writeValueAsString(all));
        return all;
    }

    @GetMapping("test1")
    public Object test1() {
        User save = userMongoMapper.save(new User("1", 2, true));

        List<User> all = userMongoMapper.findAll();
        return all;
    }

    public static void main(String[] args) {
        String slat = "&%5123***&&%%$$#@";

        String base = "Could not find artifact com.hikvision:artemis" + "/" + slat;
        String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
        System.out.println(md5);
    }


}
